package com.weijian.controller;

import com.weijian.common.result.ApiResponse;
import com.weijian.dal.entity.MatchDetail;
import com.weijian.dal.entity.MatchResult;
import com.weijian.dto.match.MatchDetailWithRouteAndShipmentDTO;
import com.weijian.dto.match.MatchResultData;
import com.weijian.service.MatchDetailService;
import com.weijian.service.MatchResultService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 匹配结果管理接口
 */
@Tag(name = "匹配结果管理", description = "匹配结果增删改查接口")
@RestController
@RequestMapping("/api/match-results")
public class MatchResultController {

    @Autowired
    private MatchResultService matchResultService;


    @Autowired
    private MatchDetailService matchDetailService;

    /**
     * 根据dataId查询匹配结果摘要
     *
     * @param dataId 数据ID
     * @return 匹配结果列表
     */
    @Operation(summary = "根据dataId查询匹配结果摘要")
    @GetMapping("/results/{dataId}")
    public ApiResponse<List<MatchResult>> getMatchResultsByDataId(
            @Parameter(description = "数据ID") @PathVariable Integer dataId) {
        List<MatchResult> matchResults = matchResultService.lambdaQuery()
                .eq(MatchResult::getDataId, dataId)
                .list();
        return ApiResponse.success(matchResults);
    }

    /**
     * 根据dataId查询匹配结果（包含匹配详情）
     *
     * @param dataId 数据ID
     * @return 匹配结果数据列表
     */
    @Operation(summary = "根据dataId查询匹配结果（包含匹配详情）")
    @GetMapping("/all/{dataId}")
    public ApiResponse<List<MatchResultData>> getMatchResultsWithDetailsByDataId(
            @Parameter(description = "数据ID") @PathVariable Integer dataId) {
        List<MatchResultData> resultDataList = matchResultService.getMatchResultsWithDetailsByDataId(dataId);
        return ApiResponse.success(resultDataList);
    }


    /**
     * 根据dataId查询匹配详情
     *
     * @param dataId 数据ID
     * @return 匹配详情列表
     */
    @Operation(summary = "根据dataId查询匹配详情")
    @GetMapping("/details/{dataId}")
    public ApiResponse<List<MatchDetail>> getMatchDetailsByDataId(
            @Parameter(description = "数据ID") @PathVariable Integer dataId) {
        List<MatchDetail> matchDetails = matchDetailService.getMatchDetailsByDataId(dataId);
        return ApiResponse.success(matchDetails);
    }

    /**
     * 根据dataId查询匹配详情（包含路线和货物信息）
     *
     * @param dataId 数据ID
     * @return 匹配详情列表（包含路线和货物信息）
     */
    @Operation(summary = "根据dataId查询匹配详情（包含路线和货物信息）")
    @GetMapping("/details-with-info/{dataId}")
    public ApiResponse<List<MatchDetailWithRouteAndShipmentDTO>> getMatchDetailsWithRouteAndShipmentByDataId(
            @Parameter(description = "数据ID") @PathVariable Integer dataId) {
        List<MatchDetailWithRouteAndShipmentDTO> matchDetails = matchDetailService.getMatchDetailsWithRouteAndShipmentByDataId(dataId);
        return ApiResponse.success(matchDetails);
    }

    /**
     * 查询某条路线匹配的匹配情况
     *
     * @param routeId 路线ID
     * @return 匹配详情列表
     */
    @Operation(summary = "查询某条路线匹配的匹配情况")
    @GetMapping("/route/{routeId}")
    public ApiResponse<List<MatchDetail>> getAllMatchDetailsByRoute(@PathVariable Integer routeId) {
        List<MatchDetail> details = matchResultService.getMatchDetailsByRouteId(routeId);
        return ApiResponse.success(details);
    }

    /**
     * 根据ID查询匹配结果
     *
     * @param id 匹配结果ID
     * @return 匹配结果
     */
    @Operation(summary = "根据ID查询匹配结果")
    @GetMapping("/{id}")
    public ApiResponse<MatchResult> getMatchResultById(
            @Parameter(description = "匹配结果ID") @PathVariable Integer id) {
        MatchResult matchResult = matchResultService.getMatchResultById(id);
        if (matchResult == null) {
            return ApiResponse.notFound("匹配结果不存在");
        }
        return ApiResponse.success(matchResult);
    }
}